package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.ActivityContent;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by gaoshan on 15-10-20.
 */
public class ActivityContentConvert {

    public static DBObject castActivityContentToDBObject(ActivityContent activityContent) {
        DBObject dbo = new BasicDBObject();
        if (activityContent.getId() != null) {
            dbo.put("_id", activityContent.getId());
        }
        dbo.put("actId", activityContent.getActId());
        dbo.put("programId", activityContent.getProgramId());
        dbo.put("pType", activityContent.getPType());
        dbo.put("priority", ObjectUtils.toLong(activityContent.getPriority(), 0L));
        dbo.put("startTime", activityContent.getStartTime());
        dbo.put("createTime", ObjectUtils.toLong(activityContent.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", System.currentTimeMillis());
        return dbo;
    }

    public static ActivityContent castDBObjectToActivityContent(DBObject dbObject) {
        ActivityContent activityContent = new ActivityContent();
        if (dbObject != null) {
            activityContent.setId(ObjUtils.toLong(dbObject.get("_id")));
            activityContent.setActId(ObjUtils.toLong(dbObject.get("actId")));
            activityContent.setProgramId(ObjUtils.toLong(dbObject.get("programId")));
            activityContent.setPType(ObjectUtils.toInteger(dbObject.get("pType")));
            activityContent.setPriority(ObjUtils.toLong(dbObject.get("priority")));
            activityContent.setStartTime(ObjUtils.toLong(dbObject.get("startTime")));
            activityContent.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            activityContent.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
        } else {
            return null;
        }
        return activityContent;
    }
}
